package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.Employee;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO 用户
 *
 * @author shaoxuan.
 * @date 2023/12/23 14:45
 */

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")

public class UserController {
    @Autowired
    private UserService service;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")

    public R register(@RequestBody User user) {
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        User one = service.getOne(new LambdaQueryWrapper<User>().
                eq(User::getUsername, user.getUsername()));
        if(one==null){
            user.setPassword(password);
            service.save(user);
            return R.success("注册成功");
        }else {
            return R.error("用户名已存在，注册失败");
        }


    }

    /**
     * 用户登录
     *
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")

    public R<User> login(HttpServletRequest request, @RequestBody User user) {
        //1、将页面提交的密码password进行md5加密处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User user1= service.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (user1 == null) {
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!user1.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //6、登录成功，将用户id存入Session并返回登录成功结果
        request.getSession().setAttribute("user", user1.getId());
        return R.success(user1);
    }


    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录用户的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }


    /**
     * 根据id获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/getInfo/{userId}")
    @ApiOperation(value = "根据id获取用户信息")

    public R<User> getInfoBy(@PathVariable("userId") String userId){
        User one = service.getOne(new LambdaQueryWrapper<User>().eq(User::getId, userId));
        return R.success(one);
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改个人信息")

    public R<String> update(@RequestBody User user){
        if (user.getId()!=null){
            service.updateById(user);
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

}
